package com.own.demo.geodemo;

import com.own.demo.geogrid.BoundingBoxUtil;
import com.own.demo.geogrid.UtilsGeoGrid;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.RegionUtil;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.opengis.geometry.BoundingBox;

import java.io.File;
import java.util.*;

public class GeoGridTestV3CenterPoint {
    static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();
    static LineBufferImpl lineBuffer= new LineBufferImpl();
    static String[] noPoint = "112.788572,28.244334;113.593521,28.182595;113.314825,28.334820;113.325724,28.037024;112.785673,28.181760;112.783130,28.167562".split("\\;");

    @Test
    public void testGeoArea() {
        System.out.println("开始执行");
        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\device.txt");
        String geoData = FileToStringUtils.txt2String(geoZJ).split("\\|")[0];
        String[] adasStr = geoData.split("\\,");
        int row = adasStr.length;
        System.out.println("行政区域围栏点数量：" + row);

        List<Double> lonList = new ArrayList(100000);
        List<Double> latList = new ArrayList(100000);

        for (int i = 0; i < row; i++) {
            String[] points = adasStr[i].split(" ");
            int pointLength = points.length;
            if (pointLength > 2) {
                System.out.println("错误采集点");
                continue;
            }

            try {
                lonList.add(Double.valueOf(points[0]));
                latList.add(Double.valueOf(points[1]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("lon长度" + lonList.size());
        System.out.println("lat长度" + latList.size());

        double minLon = Collections.min(lonList);
        double maxLon = Collections.max(lonList);
        double minLat = Collections.min(latList);
        double maxLat = Collections.max(latList);

        System.out.println("minLon="+minLon+" maxLon="+maxLon);

        System.out.println("minLat="+minLat+" maxLon="+maxLat);

        Map<Integer, String> dataMap = new HashMap();
        try {
            BoundingBox boundingBox = BoundingBoxUtil.getBoundingBox(minLon, maxLon, minLat, maxLat);
            Geometry geometry = PoiTypeEnums.geometry(geoData, PoiTypeEnums.POLYGON.getCode());
            Double sid= 0.09;///UtilsGeoGrid.getSideLen(boundingBox);
            List<Geometry> list = new UtilsGeoGrid().gridGeometryInfo(geometry, boundingBox,true,sid);
            System.out.println("DELETE from geofence where  service_id=713;");

            int count = list.size();
            for (int i = 0; i < count; i++) {
                Polygon polygon = (Polygon) list.get(i);

                getSql(polygon, i);

            }

            System.out.println();

            for (Map.Entry<Integer, String> set : dataMap.entrySet()) {
                System.out.println(set.getKey() + " " + set.getValue());
            }

            //线路1 28 54 55 80 103 104 105 83 84 61 84 87 88  88 89 91 93
            //线路2 19 41 64 63  86 61
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //116.259506,39.961547;116.258559,39.961337;116.258539,39.960595;116.259527,39.960343;116.260514,39.960269;116.261578,39.961470;116.261637,39.960788
    public static String getSql(Polygon polygon, int h) {
        String data = polygonBuffer.gpsToString(polygon.getCoordinates());
        for (int i = 0; i < noPoint.length; i++) {
            String point = noPoint[i];
            if (RegionUtil.polygonCheck(point, data)) {
                return "";
            }
        }
        Point point = polygon.getCentroid();
        String center = point.getX() + "," + point.getY();

        String sql = "INSERT INTO `geofence` (" +
                "`service_id`," +
                "`ak`," +
                "`cert_info_id`," +
                "`geofence_name`," +
                "`geofence_shape`," +
                "`coordinates_buffer`," +
                "`end_time`," +
                "`start_time`," +
                "`deleted`," +
                "`group_id`," +
                "`fence_create_type`," +
                "`coordinates`," +
                "`center`," +
                "`radius`," +
                "`poi_id`," +
                "`bounds`," +
                "`buffer`," +
                "`judge_type`," +
                "`status`," +
                "`audit_info`," +
                "`money`," +
                "`audit_time`," +
                "`audit_user_id`," +
                "`create_time`," +
                "`warning_count`," +
                "`geometry_info`," +
                "`adcode`," +
                "`max_geometry` " +
                ")" +
                "VALUES" +
                "(" +
                "" +
                "713," +
                "'FD1635824265606119417A4631F30DB04342A86E12D485369E64NSZETCMM7254'," +
                "254," +
                "'" + h + "'," +
                "'polygon'," +
                "'" + data + "'," +
                "NULL," +
                "NULL," +
                "0," +
                "NULL," +
                "'0'," +
                "'" + data + "'," +
                "'" + center + "'," +
                "NULL," +
                "NULL," +
                "NULL," +
                "0.00," +
                "NULL," +
                "0," +
                "NULL," +
                "0.00," +
                "NULL," +
                "NULL," +
                "'2022-07-16 16:45:46'," +
                "0," +
                "ST_GeomFromText ( '" + polygon.toString() + "' )," +
                "NULL," +
                "NULL " +
                ");";
        System.out.println(sql);
        return sql;
    }
}
